package com.feidee.data.report.util.mail;

import com.alibaba.fastjson.JSONObject;
import com.feidee.data.email.model.EConstant;
import com.feidee.data.report.constant.Constant;
import com.feidee.data.report.model.ReportLogModel;
import com.feidee.data.report.model.mail.ReceiverMessModel;
import com.feidee.data.report.model.mail.ReportMessModel;
import com.feidee.data.report.util.TimeUtil;
import com.feidee.data.report.util.ToolUtils;
import com.feidee.fdcommon.configuration.CustomConfiguration;
import com.feidee.fdhadoop.logCollect.LogCollectService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class RMailSender {

    private static Logger logger = Logger.getLogger(RMailSender.class);

    /**
     * 以文本格式发送邮件
     *
     * @param mailInfo 待发送的邮件的信息
     */
    public static boolean sendTextMail(RMailInfo mailInfo) {
        RAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        if (mailInfo.isValidate()) {
            authenticator = new RAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
        }
        Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
        try {
            Message mailMessage = new MimeMessage(sendMailSession);
            Address from = new InternetAddress(mailInfo.getFromAddress());
            mailMessage.setFrom(from);
            Address to = new InternetAddress(mailInfo.getToAddress());
            mailMessage.setRecipient(Message.RecipientType.TO, to);
            mailMessage.setSubject(mailInfo.getSubject());
            mailMessage.setSentDate(new Date());
            String mailContent = mailInfo.getContent();
            mailMessage.setText(mailContent);
            Transport.send(mailMessage);
            return true;
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * 以HTML格式发送邮件
     *
     * @param mailInfo 待发送的邮件信息
     */
    public static boolean sendHtmlMail(RMailInfo mailInfo) {
        RAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        if (mailInfo.isValidate()) {
            authenticator = new RAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
        }
        Session sendMailSession = Session.getInstance(pro, authenticator);
        try {
            Message mailMessage = new MimeMessage(sendMailSession);
            Address from = new InternetAddress(mailInfo.getFromAddress());
            mailMessage.setFrom(from);
            Address to = new InternetAddress(mailInfo.getToAddress());
            mailMessage.setRecipient(Message.RecipientType.TO, to);
            mailMessage.setSubject(mailInfo.getSubject());
            mailMessage.setSentDate(new Date());
            Multipart mainPart = new MimeMultipart();
            BodyPart html = new MimeBodyPart();
            html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
            mainPart.addBodyPart(html);
            mailMessage.setContent(mainPart);
            Transport.send(mailMessage);
            return true;
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public static boolean sendHtmlMail(String title, String content, String reciver) throws AddressException {
        RMailInfo mailInfo = new RMailInfo();
        mailInfo.setMailServerHost(EConstant.EMAIL_SEND_HOST);
        mailInfo.setMailServerPort(EConstant.EMAIL_SEND_PORT);
        mailInfo.setValidate(true);
        mailInfo.setUserName(EConstant.EMAIL_SEND_USER);
        mailInfo.setPassword(EConstant.EMAIL_SEND_PASSWORD);
        mailInfo.setFromAddress(EConstant.EMAIL_SEND_FROM);

        //mailInfo.setToAddress(Constant.EMAIL_TO);
        String[] to = reciver.split(";", -1);
        InternetAddress[] sendTo = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) {
            sendTo[i] = new InternetAddress(to[i]);
        }

        mailInfo.setSubject(title);
        mailInfo.setContent(content);

        RAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        if (mailInfo.isValidate()) {
            authenticator = new RAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
        }
        Session sendMailSession = Session.getInstance(pro, authenticator);
        try {
            Message mailMessage = new MimeMessage(sendMailSession);
            Address from = new InternetAddress(mailInfo.getFromAddress());
            mailMessage.setFrom(from);
            mailMessage.setRecipients(Message.RecipientType.TO, sendTo);
            mailMessage.setSubject(mailInfo.getSubject());
            mailMessage.setSentDate(new Date());
            Multipart mainPart = new MimeMultipart();
            BodyPart html = new MimeBodyPart();
            html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
            mainPart.addBodyPart(html);
            mailMessage.setContent(mainPart);
            Transport.send(mailMessage);
            return true;
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public static boolean sendTextMail(String title, String content) {
        RMailInfo mailInfo = new RMailInfo();
        mailInfo.setMailServerHost(Constant.EMAIL_SERVER);
        mailInfo.setMailServerPort(Constant.EMAIL_PORT);
        mailInfo.setValidate(true);
        mailInfo.setUserName(Constant.EMAIL_USER);
        mailInfo.setPassword(Constant.EMAIL_PWD);//您的邮箱密码   
        mailInfo.setFromAddress(Constant.EMAIL_FROM);
        mailInfo.setSubject(title);
        mailInfo.setContent(content);
        //mailInfo.setToAddress(Constant.EMAIL_TO);   

        RAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        if (mailInfo.isValidate()) {
            authenticator = new RAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
        }
        Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
        try {
            Message mailMessage = new MimeMessage(sendMailSession);
            Address from = new InternetAddress(mailInfo.getFromAddress());
            mailMessage.setFrom(from);
            Address to = new InternetAddress(mailInfo.getToAddress());
            mailMessage.setRecipient(Message.RecipientType.TO, to);
            mailMessage.setSubject(mailInfo.getSubject());
            mailMessage.setSentDate(new Date());
            String mailContent = mailInfo.getContent();
            mailMessage.setText(mailContent);
            for (String email : Constant.EMAIL_TO) {
                mailInfo.setToAddress(email);
                Transport.send(mailMessage);
            }
            return true;
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public static void sendReportMail(String reportId, int status) {
        String url = EConstant.SEND_REPORT_MAIL_URL;
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = null;
        int attemptTimes = 3;
        url += "?reportId=" + reportId + "&status=" + status;
        HttpGet get = new HttpGet(url);
        ReportLogModel logs = new ReportLogModel();
        logs.setReport_key(reportId);
        logs.setReport_index("Send Email");
        for (int i = 1; i <= attemptTimes; i++) {
            try {
                if (i == attemptTimes) {
                    Thread.sleep(1000 * 60L);// 第三次重试时，休眠1分钟
                }
                response = client.execute(get);
                break;
            } catch (Exception e) {
                if (i == attemptTimes) {
                    e.printStackTrace();
                    logs.setStatus("Connect " + Constant.REPORT_MAIL_EXCEPTION);
                    logs.setTs(TimeUtil.getNow(Constant.LOG_TIME_FORMAT));
                    LogCollectService.sendLog(CustomConfiguration.getString(Constant.LOG_APT),
                            CustomConfiguration.getString(Constant.LOG_BUS), logs);
                    logger.info("报表 " + reportId + " 连接邮件订阅服务失败!");
                }
            }
        }
        HttpEntity entity = null;
        if (response != null) {
            entity = response.getEntity();
        }
        InputStream in;
        try {
            in = entity.getContent();
            String str = ToolUtils.convertStreamToString(in);
            if (str.contains("success")) {
                logs.setStatus(Constant.REPORT_MAIL_SUCCESS);
                logs.setTs(TimeUtil.getNow(Constant.LOG_TIME_FORMAT));
                LogCollectService.sendLog(CustomConfiguration.getString(Constant.LOG_APT),
                        CustomConfiguration.getString(Constant.LOG_BUS), logs);
                logger.info("报表 " + reportId + " 发送订阅邮件成功!");
            } else if (str.contains("failed")) {
                logs.setStatus(Constant.REPORT_MAIL_EXCEPTION);
                logs.setTs(TimeUtil.getNow(Constant.LOG_TIME_FORMAT));
                LogCollectService.sendLog(CustomConfiguration.getString(Constant.LOG_APT),
                        CustomConfiguration.getString(Constant.LOG_BUS), logs);
                logger.info("报表 " + reportId + " 发送订阅邮件失败!");
            } else {
                logs.setStatus("Received " + Constant.REPORT_MAIL_EXCEPTION);
                logs.setTs(TimeUtil.getNow(Constant.LOG_TIME_FORMAT));
                LogCollectService.sendLog(CustomConfiguration.getString(Constant.LOG_APT),
                        CustomConfiguration.getString(Constant.LOG_BUS), logs);
                logger.info("报表 " + reportId + " 订阅邮件服务返回值异常!返回值为: " + str);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logs.setStatus("Received " + Constant.REPORT_MAIL_EXCEPTION);
            logs.setTs(TimeUtil.getNow(Constant.LOG_TIME_FORMAT));
            LogCollectService.sendLog(CustomConfiguration.getString(Constant.LOG_APT),
                    CustomConfiguration.getString(Constant.LOG_BUS), logs);
            logger.info("报表 " + reportId + " 订阅邮件服务返回值围绕false!");
        }
    }

    public static void sendReportExceptionMail(ResultSet remainReport, Map<String, Long> exceptionReport,
                                               Map<String, Long> successReport, List<String> successReportList, boolean isSendHandler, boolean isSendAll) throws AddressException {
        Map<String, List<String>> exceptionMap = null;
        ReceiverMessModel receiverMessModel = null;
        String title = "报表执行监控汇总";
        if (!exceptionReport.isEmpty()) {
            title = "报表执行出错预警汇总";
        }
        if (!isSendHandler) {
            title = "报表定时预警汇总";
        }
        if (isSendAll) {
            title = "所有报表执行监控汇总";
        }
        String reportKey;
        String message;
        String costTime;
        String endTime;
        String priority;
        String createUser;
        StringBuffer contentBuf = new StringBuffer(
                "<p class=\"MsoNormal\"  align=\"left\"  style=\"text-align: justify; margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"><o:p>&nbsp;</o:p></span></p>\n"
                        + "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">各位同事好，<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \n"
                        + "<o:p></o:p></span></p>\n"
                        + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">截止北京时间<span lang=\"EN-US\">"
                        + TimeUtil.getNow("yyyy-MM-dd HH:mm")
                        + "</span><span lang=\"EN-US\"></span><span style=\"color:black\">，</span><span style=\"color:black\">以下报表未能成功执行</span><span lang=\"EN-US\"  style=\"color:red\"><o:p></o:p></span></span></p>\n"
                        + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ;;color:black \">请开发尽快处理，</span><span style=\"  ; ; ;;color:black \">谢谢<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \"><o:p>&nbsp;</o:p></span></p>\n"
                        + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">附：<span style=\"  ; ; ;;color:red \">出错报表信息</span><span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "<table class=\"MsoNormalTable\"  border=\"0\"  cellspacing=\"0\"  cellpadding=\"0\"  width=\"531\"  style=\"width:900pt;margin-left:18.0pt;border-collapse:collapse\">\n"
                        + "<tbody>\n"
                        + "<tr style=\"height:2.85pt\">\n"
                        + "<td width=\"10\"  style=\"width:80pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">序号<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"116\"  style=\"width:180pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">报表名称<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"116\"  style=\"width:100pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">负责人<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"416\"  style=\"width:410pt;border:solid windowtext 1.0pt;background:#95B3D7;border-left:none;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">错误信息<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"116\"  style=\"width:160pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">执行耗时<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"116\"  style=\"width:160pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">出错时间<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"116\"  style=\"width:100pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 8pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">优先级<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "</tr>\n");
        if (!exceptionReport.isEmpty()) {
            exceptionReport = ToolUtils.sortMapByValue(exceptionReport);
            exceptionMap = new HashMap<String, List<String>>();
            int exceptionNum = 1;
            for (String key : exceptionReport.keySet()) {
                costTime = ToolUtils.getCostTimeStr(exceptionReport.get(key));
                String[] reportMessages = key.split("€", -1);
                reportKey = reportMessages[0];
                message = reportMessages[1];
                endTime = reportMessages[2];
                priority = reportMessages[3];
                //如果报表执行成功忽略掉这条异常记录
                if (successReportList.contains(reportKey)) {
                    continue;
                }

                receiverMessModel = getReceiverMess(reportKey);

                if (receiverMessModel != null && receiverMessModel.getStatus().equals("true") && receiverMessModel.getData() != null
                        && receiverMessModel.getData().size() >=1 && ToolUtils.isNotNull(receiverMessModel.getData().get(0).getReportCnName())) {
                    reportKey += "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + receiverMessModel.getData().get(0).getReportCnName();
                }
                if (receiverMessModel != null && receiverMessModel.getStatus().equals("true") && receiverMessModel.getData() != null
                        && receiverMessModel.getData().size() >=1 && ToolUtils.isNotNull(receiverMessModel.getData().get(0).getCreateUser())) {
                    createUser = receiverMessModel.getData().get(0).getCreateUser();
                } else {
                    createUser = "UNKNOWN";
                }

                addExceptionReceiver(exceptionMap, receiverMessModel, reportKey, message, endTime, costTime, priority);

                contentBuf.append("<tr style=\"height:2.85pt\">\n"
                        + "<td width=\"10\"  style=\"width:80pt;border:solid windowtext 1.0pt;border-top:none;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");

                contentBuf.append(exceptionNum);
                contentBuf.append("<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"116\"  style=\"width:100pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(reportKey);
                contentBuf.append("<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"116\"  style=\"width:100pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(createUser);

                contentBuf.append("<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"416\"  style=\"width:350pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(message);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"416\"  style=\"width:175pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(costTime);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"416\"  style=\"width:175pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(endTime);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"416\"  style=\"width:100pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 8pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(priority);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "</tr>\n");
                exceptionNum++;
            }
        }
        contentBuf.append("</tbody>\n"
                + "</table>"
                + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \"><o:p>&nbsp;</o:p></span></p>"
                + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">附：<span style=\"  ; ; ;;color:green \">成功报表信息</span><span lang=\"EN-US\"><o:p></o:p></span></span></p>"
                + "<table class=\"MsoNormalTable\"  border=\"0\"  cellspacing=\"0\"  cellpadding=\"0\"  width=\"531\"  style=\"width:900pt;margin-left:18.0pt;border-collapse:collapse\">\n"
                + "<tbody>\n"
                + "<tr style=\"height:2.85pt\">\n"
                + "<td width=\"116\"  style=\"width:200pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">报表名称<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                + "</td>\n"
                + "<td width=\"416\"  valign=\"top\"  style=\"width:175pt;border:solid windowtext 1.0pt;background:#95B3D7;border-left:none;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">负责人<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                + "</td>\n"
                + "<td width=\"416\"  valign=\"top\"  style=\"width:175pt;border:solid windowtext 1.0pt;background:#95B3D7;border-left:none;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">执行信息<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                + "</td>\n"
                + "<td width=\"116\"  style=\"width:175pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">执行耗时<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                + "</td>\n"
                + "<td width=\"116\"  style=\"width:175pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">结束时间<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                + "</td>\n"
                + "<td width=\"116\"  style=\"width:70pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                + "<p class=\"MsoListParagraph\"  style=\"text-indent: 8pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">优先级<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                + "</td>\n"
                + "</tr>\n");
        if (!successReport.isEmpty()) {
            successReport = ToolUtils.sortMapByValue(successReport);
            int reportNum = 0;
            for (String key : successReport.keySet()) {
                if (!isSendAll) {
                    if (reportNum == 50) {
                        break;
                    }
                }
                costTime = ToolUtils.getCostTimeStr(successReport.get(key));
                String[] reportMessages = key.split(";", -1);
                reportKey = reportMessages[0];
                message = reportMessages[1];
                endTime = reportMessages[2];
                priority = reportMessages[3];

                receiverMessModel = getReceiverMess(reportKey);

                if (receiverMessModel != null && receiverMessModel.getStatus().equals("true") && receiverMessModel.getData() != null
                        && receiverMessModel.getData().size() >=1 &&  ToolUtils.isNotNull(receiverMessModel.getData().get(0).getReportCnName())) {
                    reportKey += "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + receiverMessModel.getData().get(0).getReportCnName();
                }
                if (receiverMessModel != null && receiverMessModel.getStatus().equals("true") && receiverMessModel.getData() != null
                        && receiverMessModel.getData().size() >=1 && ToolUtils.isNotNull(receiverMessModel.getData().get(0).getCreateUser())) {
                    createUser = receiverMessModel.getData().get(0).getCreateUser();
                } else {
                    createUser = "UNKNOWN";
                }

                contentBuf.append("<tr style=\"height:2.85pt\">\n"
                        + "<td width=\"200\"  style=\"width:200pt;border:solid windowtext 1.0pt;border-top:none;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(reportKey);
                contentBuf.append("<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"175\"  valign=\"top\"  style=\"width:175pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(createUser);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "</td>\n"
                        + "<td width=\"350\"  valign=\"top\"  style=\"width:175pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(message);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"175\"  valign=\"top\"  style=\"width:175pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(costTime);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"175\"  valign=\"top\"  style=\"width:175pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(endTime);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"175\"  valign=\"top\"  style=\"width:70pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 8pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(priority);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "</tr>\n");
                reportNum++;
            }
        }
        try {
            if (remainReport != null) {
                contentBuf.append("</tbody>\n" + "</table>\n"
                        + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \"><o:p>&nbsp;</o:p></span></p>"
                        + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">附：<span style=\"  ; ; ;;color:red \">未执行报表</span><span lang=\"EN-US\"><o:p></o:p></span></span></p>"
                        + "<table class=\"MsoNormalTable\"  border=\"0\"  cellspacing=\"0\"  cellpadding=\"0\"  width=\"531\"  style=\"width:900pt;margin-left:18.0pt;border-collapse:collapse\">\n"
                        + "<tbody>\n"
                        + "<tr style=\"height:2.85pt\">\n"
                        + "<td width=\"10\"  style=\"width:80pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">序号<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"116\"  style=\"width:300pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">报表名称<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"116\"  style=\"width:300pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">报表状态<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"116\"  style=\"width:300pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">优先级<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "</tr>\n");
                int remainNum = 1;
                while (remainReport.next()) {
                    String remainReportKey = remainReport.getString("report_key");

                    receiverMessModel = getReceiverMess(remainReportKey);

                    if (receiverMessModel != null && receiverMessModel.getStatus().equals("true") && receiverMessModel.getData() != null
                            && receiverMessModel.getData().size() >=1 && ToolUtils.isNotNull(receiverMessModel.getData().get(0).getReportCnName())) {
                        remainReportKey += "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + receiverMessModel.getData().get(0).getReportCnName();
                    }
                    contentBuf.append("<tr style=\"height:2.85pt\">\n"
                            + "<td width=\"10\"  style=\"width:80pt;border:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                            + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">"
                            + remainNum
                            + "<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                            + "</td>\n"
                            + "<td width=\"116\"  style=\"width:300pt;border:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                            + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">"
                            + remainReportKey
                            + "<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                            + "</td>\n"
                            + "<td width=\"116\"  style=\"width:300pt;border:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                            + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">"
                            + remainReport.getString("exec_status")
                            + "<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                            + "</td>\n"
                            + "<td width=\"116\"  style=\"width:300pt;border:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                            + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">"
                            + ToolUtils.getPriorityLevel(remainReport.getInt("priority"))
                            + "<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                            + "</td>\n"
                            + "</tr>\n");
                    remainNum++;
                }
            }
        } catch (SQLException e) {
            logger.error("##### 生成未完成报表错误 #####");
        }
        contentBuf.append("</tbody>\n" + "</table>\n"
                + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \"><o:p>&nbsp;</o:p></span></p>\n"
                + "<div class=\"MsoNormal\"  align=\"left\"  style=\"text-align: justify; margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\">\n"
                + "<hr size=\"1\"  width=\"210\"  style=\"width:157.5pt\"  noshade=\"\"  align=\"left\" />\n"
                + "</span></div>\n"
                + "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"color:#7F7F7F\"><a href=\"http://data.feidee.com/bigdata/own/index/index.jsp\"  style=\"color: blue; text-decoration: underline;\"><span lang=\"EN-US\"  style=\"font-family:宋体;color:#7F7FFF\"><span lang=\"EN-US\">随手科技数据门户</span></span></a><o:p></o:p></span></p>\n"
                + "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\"font-family:宋体;color:#7F7F7F\">大数据云事业部</span><span style=\"color:#7F7F7F\">\n"
                + "<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                + "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"><o:p>&nbsp;</o:p></span></p>\n"
                + "</div>\n" + "</div></div>");
        String content = contentBuf.toString();

        if (exceptionMap != null && !exceptionMap.isEmpty() && isSendHandler == true) {
            sendExceptionEmailToHandler(exceptionMap);
        }
        if (successReport.size() <= 10) {
            logger.info("#### 非定时执行任务，无需发送预警邮件 ####");
            return;
        }
        if (isSendAll) {
            sendHtmlMail(title, content, CustomConfiguration.getString("all.report.product.receiver"));
        } else {
            sendHtmlMail(title, content, EConstant.Exception_REPORT_MAIL_GATHER_RECEIVER);
        }
    }

    private static void addExceptionReceiver(Map<String, List<String>> exceptionMap, ReceiverMessModel receiverMessModel, String reportKey, String message, String endTime, String costTime, String priority) {
        try {
            if (receiverMessModel != null && receiverMessModel.getStatus().equals("true") && receiverMessModel.getData() != null) {
                List<ReportMessModel> data = receiverMessModel.getData();
                for (ReportMessModel reportMessModel : data) {
                    String mailbox = EConstant.Exception_REPORT_MAIL_HANDLER_RECEIVER;
                    String createUser = "未知负责人";
                    String reportMessModelMailbox = reportMessModel.getMailbox();
                    String reportMessModelCreateUser = reportMessModel.getCreateUser();
                    if (ToolUtils.isNotNull(reportMessModelMailbox)) {
                        mailbox = reportMessModelMailbox;
                    }
                    if (ToolUtils.isNotNull(reportMessModelCreateUser)) {
                        createUser = reportMessModelCreateUser;
                    }
                    String key = mailbox + "€" + createUser;
                    String value = reportKey + "€" + message + "€" + endTime + "€" + costTime + "€" + priority;
                    List<String> valuesList = new ArrayList<>();
                    valuesList.add(value);
                    if (!Constant.ENV.equals("live") && !ToolUtils.isNotNull(mailbox)) {
                        return;
                    }
                    if (exceptionMap.containsKey(key)) {
                        List<String> stringList = exceptionMap.get(key);
                        stringList.add(value);
                    } else {
                        exceptionMap.put(key, valuesList);
                    }
                }
            } else {
                String mailbox = EConstant.Exception_REPORT_MAIL_HANDLER_RECEIVER;
                String createUser = "未知负责人";
                String key = mailbox + "€" + createUser;
                String value = reportKey + "€" + message + "€" + endTime + "€" + costTime + "€" + priority;
                List<String> valuesList = new ArrayList<>();
                valuesList.add(value);
                if (!Constant.ENV.equals("live") && !ToolUtils.isNotNull(mailbox)) {
                    return;
                }
                if (exceptionMap.containsKey(key)) {
                    List<String> stringList = exceptionMap.get(key);
                    stringList.add(value);
                } else {
                    exceptionMap.put(key, valuesList);
                }
            }
        } catch (Exception e) {
            logger.error("添加异常报表信息异常", e);
        }
    }

    public static ReceiverMessModel getReceiverMess(String reportKey) {
        HttpClient client;
        HttpGet get;
        HttpResponse response;
        HttpEntity entity;
        ReceiverMessModel receiverMessModel = null;
        InputStream in = null;
        try {
            logger.info("##### 开始获取" + reportKey + "负责人信息");
            client = new DefaultHttpClient();
            String url = Constant.EMAIL_GET_RECEIVER_URL + "?sqlNames=" + reportKey;
            get = new HttpGet(url);
            response = client.execute(get);
            entity = null;
            if (response != null) {
                entity = response.getEntity();
            }
            in = entity.getContent();
            String str = ToolUtils.convertStreamToString(in);
            JSONObject jsonObject = JSONObject.parseObject(str);
            receiverMessModel = JSONObject.toJavaObject(jsonObject, ReceiverMessModel.class);
            if (receiverMessModel != null && receiverMessModel.getStatus().equals("true")) {
                logger.info("##### 报表 " + reportKey + " 获取负责人信息成功");
            } else {
                logger.info("##### 报表 " + reportKey + " 没有负责人信息");
            }
        } catch (Exception e) {
            logger.info("##### 报表 " + reportKey + " 获取负责人信息异常", e);
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return receiverMessModel;
    }

    public static void sendExceptionEmailToHandler(Map<String, List<String>> exceptionMap) {
        String mailbox, createUser, reportKey, message, endTime, costTime, priority;
        for (String key : exceptionMap.keySet()) {
            String[] keySplit = key.split("€", -1);
            mailbox = keySplit[0];
            createUser = keySplit[1];
            String title = "报表执行出错预警";
            StringBuffer contentBuf = new StringBuffer(
                    "<p class=\"MsoNormal\"  align=\"left\"  style=\"text-align: justify; margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"><o:p>&nbsp;</o:p></span></p>\n"
                            + "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">"
                            + createUser + "，你好："
                            + "<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                            + "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \n"
                            + "<o:p></o:p></span></p>\n"
                            + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">截止北京时间<span lang=\"EN-US\">"
                            + TimeUtil.getNow("yyyy-MM-dd HH:mm")
                            + "</span><span lang=\"EN-US\"></span><span style=\"color:black\">，</span><span style=\"color:black\">你所负责的以下报表未能成功执行</span><span lang=\"EN-US\"  style=\"color:red\"><o:p></o:p></span></span></p>\n"
                            + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ;;color:black \">"
                            + "请尽快处理，</span><span style=\"  ; ; ;;color:black \">谢谢<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                            + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \"><span lang=\"EN-US\">"
                            + "</span><span lang=\"EN-US\"></span><span style=\"color:black\"></span><span style=\"color:black\"><span style=\"  ; ; ;;color:black \">解决方案请参考：</span><a href=\"http://172.22.23.174:8091/pages/viewpage.action?pageId=6525927\"  style=\"color: blue; text-decoration: underline;\"><span lang=\"EN-US\"  style=\"font-family:宋体;color:#7F7FFF\"><span lang=\"EN-US\">预警邮件常见原因说明</span></span></a></span><span lang=\"EN-US\"  style=\"color:red\"><o:p></o:p></span></span></p>\n"
                            + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \"><o:p>&nbsp;</o:p></span></p>\n"
                            + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">附：<span style=\"  ; ; ;;color:red \">出错报表信息</span><span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                            + "<table class=\"MsoNormalTable\"  border=\"0\"  cellspacing=\"0\"  cellpadding=\"0\"  width=\"531\"  style=\"width:900pt;margin-left:18.0pt;border-collapse:collapse\">\n"
                            + "<tbody>\n"
                            + "<tr style=\"height:2.85pt\">\n"
                            + "<td width=\"116\"  style=\"width:200pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                            + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">报表名称<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                            + "</td>\n"
                            + "<td width=\"416\"  valign=\"top\"  style=\"width:350pt;border:solid windowtext 1.0pt;background:#95B3D7;border-left:none;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                            + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">错误信息<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                            + "</td>\n"
                            + "<td width=\"116\"  style=\"width:175pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                            + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">执行耗时<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                            + "</td>\n"
                            + "<td width=\"116\"  style=\"width:175pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                            + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">出错时间<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                            + "</td>\n"
                            + "<td width=\"116\"  style=\"width:70pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                            + "<p class=\"MsoListParagraph\"  style=\"text-indent: 8pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">优先级<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                            + "</td>\n"
                            + "</tr>\n");
            List<String> valuesList = exceptionMap.get(key);
            Iterator<String> valuesIterator = valuesList.iterator();
            while (valuesIterator.hasNext()) {
                String value = valuesIterator.next();
                String[] valueSplit = value.split("€", -1);
                reportKey = valueSplit[0];
                message = valueSplit[1];
                endTime = valueSplit[2];
                costTime = valueSplit[3];
                priority = valueSplit[4];
                contentBuf.append("<tr style=\"height:2.85pt\">\n"
                        + "<td width=\"116\"  style=\"width:200pt;border:solid windowtext 1.0pt;border-top:none;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(reportKey);
                contentBuf.append("<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"416\"  style=\"width:350pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(message);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"416\"  style=\"width:175pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(costTime);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"416\"  style=\"width:175pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(endTime);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "<td width=\"416\"  style=\"width:70pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
                        + "<p class=\"MsoListParagraph\"  style=\"text-indent: 8pt; margin: 0.1cm 0cm 0.1cm; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:12pt ; ; ; ; \">");
                contentBuf.append(priority);
                contentBuf.append("<span lang=\"EN-US\"> \n"
                        + "<o:p></o:p></span></span></p>\n"
                        + "</td>\n"
                        + "</tr>\n");
            }
            contentBuf.append("</tbody>\n" + "</table>\n"
                    + "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \"><o:p>&nbsp;</o:p></span></p>\n"
                    + "<div class=\"MsoNormal\"  align=\"left\"  style=\"text-align: justify; margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\">\n"
                    + "<hr size=\"1\"  width=\"210\"  style=\"width:157.5pt\"  noshade=\"\"  align=\"left\" />\n"
                    + "</span></div>\n"
                    + "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"color:#7F7F7F\"><a href=\"http://data.feidee.com/bigdata/own/index/index.jsp\"  style=\"color: blue; text-decoration: underline;\"><span lang=\"EN-US\"  style=\"font-family:宋体;color:#7F7FFF\"><span lang=\"EN-US\">随手科技数据门户</span></span></a><o:p></o:p></span></p>\n"
                    + "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span style=\"font-family:宋体;color:#7F7F7F\">大数据云事业部</span><span style=\"color:#7F7F7F\">\n"
                    + "<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
                    + "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"><o:p>&nbsp;</o:p></span></p>\n"
                    + "</div>\n" + "</div></div>");
            String content = contentBuf.toString();
            try {
                sendHtmlMail(title, content, mailbox);
                logger.info("##### 收件人 " + createUser + " 发送预警成功");
            } catch (Exception e) {
                logger.info("##### 收件人 " + createUser + " 发送预警异常", e);
            }
        }
    }

    public static void sendOvertimeSqlToHandler(String reportKey, String index) {
        ReceiverMessModel receiverMessModel = getReceiverMess(reportKey);
        String title = "SQL运行超时通知";
        if (receiverMessModel != null && receiverMessModel.getStatus().equals("true") && receiverMessModel.getData() != null) {
            List<ReportMessModel> data = receiverMessModel.getData();
            for (ReportMessModel reportMessModel : data) {
                String mailbox = reportMessModel.getMailbox();
                if (ToolUtils.isNotNull(mailbox)) {
                    String createUser = "UNKNOWN";
                    String reportMessModelReportCnName = reportMessModel.getReportCnName();
                    String reportMessModelCreateUser = reportMessModel.getCreateUser();
                    if (ToolUtils.isNotNull(reportMessModelReportCnName)) {
                        reportKey += "(" + reportMessModelReportCnName + ")";
                    }
                    if (ToolUtils.isNotNull(reportMessModelCreateUser)) {
                        createUser = reportMessModelCreateUser;
                    }
                    String content = createUser + "，你好！你负责的报表: " + reportKey + " 第: " + index
                            + " 条SQL由于运行时间超过最长允许执行时间: " + Constant.HIVE_RUN_OVERTIME
                            + " 分钟，已被程序自动停止执行该SQL，请尽快优化SQL缩短执行时间，"
                            + "避免影响报表正常运行，谢谢配合！（如确有特殊情况请联系产品确认）";
                    try {
                        sendHtmlMail(title, content, mailbox);
                        logger.info("##### 收件人 " + createUser + " 发送SQL运行超时通知成功");
                    } catch (Exception e) {
                        logger.info("##### 收件人 " + createUser + " 发送SQL运行超时通知异常", e);
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws AddressException, InterruptedException {
//        String a = "{\n" +
//                "\t\"fid\": 0,\n" +
//                "\t\"status\": true,\n" +
//                "\t\"data\": {\n" +
//                "\t\t\"createUser\": \"邓于望\",\n" +
//                "\t\t\"applyname\": \"下线维度1\",\n" +
//                "\t\t\"mailbox\": \"yuwang_deng@suishouji.com\"\n" +
//                "\t},\n" +
//                "\t\"msg\": \"获取成功\"\n" +
//                "}";
//        JSONObject jsonObject = JSONObject.parseObject(a);
//        ReceiverMessModel stu = (ReceiverMessModel) JSONObject.toJavaObject(jsonObject, ReceiverMessModel.class);
//        System.out.println(stu);
//        Map<String, Long> exceptionReport = new HashMap<String, Long>();
//        exceptionReport.put("zkj;出错;20176;0", 52123L);
//        exceptionReport.put("zkj1;风刀霜剑立刻;20165;2", 132123L);
//        exceptionReport.put("zkj2;发送;20155;1", 465123L);
//        exceptionReport.put("zkj3;反倒是;201010;1", 53465123L);
//        Map<String, Long> successReport = new HashMap<String, Long>();
//        successReport.put("zkj;成功;20176;0", 52123L);
//        successReport.put("zkj1;成功;20165;2", 1123L);
//        successReport.put("zkj2;成功;20155;1", 53L);
//        successReport.put("zkj2;成功;20155;1", 65123L);
//        sendReportExceptionMail(null,exceptionReport,successReport);
//        System.out.println(successReport);
//        System.out.println(TimeUtil.getNow("HH:mm"));
//        ArrayBlockingQueue<String> a = new ArrayBlockingQueue<String>(10);
//        a.put("zkj1");
//        a.put("zkj2");
//        a.put("zkj3");
//        a.put("zkj4");
//        String s = a.toString();
//        String objects = a.toArray().toString();
//        System.out.println(objects);
//
//
//        ArrayBlockingQueue<ArrayList<ReportConfiguration>> remainReportArrayBlockingQueue = new ArrayBlockingQueue<ArrayList<ReportConfiguration>>(10);
//        ArrayList<ReportConfiguration> reportArrayList1 = new ArrayList<ReportConfiguration>();
//        ArrayList<ReportConfiguration> reportArrayList2 = new ArrayList<ReportConfiguration>();
//        ReportConfiguration reportConfiguration1 = new ReportConfiguration();
//        ReportConfiguration reportConfiguration2 = new ReportConfiguration();
//        reportConfiguration1.setReport_key("zkj");
//        reportConfiguration1.setReport_index("11");
//        reportConfiguration2.setReport_key("laji");
//        reportConfiguration2.setReport_index("44");
//        reportArrayList1.add(reportConfiguration1);
//        reportArrayList1.add(reportConfiguration2);
//        remainReportArrayBlockingQueue.put(reportArrayList1);
//
//
//        ReportConfiguration reportConfiguration3 = new ReportConfiguration();
//        ReportConfiguration reportConfiguration4 = new ReportConfiguration();
//        reportConfiguration3.setReport_key("zkj_2");
//        reportConfiguration3.setReport_index("11_2");
//        reportConfiguration4.setReport_key("laji_2");
//        reportConfiguration4.setReport_index("44_2");
//        reportArrayList2.add(reportConfiguration3);
//        reportArrayList2.add(reportConfiguration4);
//        remainReportArrayBlockingQueue.put(reportArrayList2);
//        Object[] objects = remainReportArrayBlockingQueue.toArray();
//        for (int i = 0; i < objects.length; i++) {
//            List<ReportConfiguration> object = (List<ReportConfiguration>) objects[i];
//            for (int j = 0; j < object.size(); j++) {
//                System.out.println("di  " + i + "   " + j + "  tiao:   " + object.get(j).getReport_key() + "      " + object.get(j).getReport_index());
//            }
//        }
//
//        sendReportExceptionMail(remainReportArrayBlockingQueue,exceptionReport, successReport);
    }
}
